from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as e
from selenium import webdriver
from datetime import datetime

import logging

logging.basicConfig(format='',level=logging.DEBUG,filename='',filemode='a+')

class Base():
    #初始化浏览器驱动
    def __init__(self, driver):
        self.driver = driver

    #封装浏览器地址
    def open_url(self, url):
        self.driver.get(url)

    #封装页面定位元素
    def find_element(self, loc):
        try:
            #显示等待
            WebDriverWait(self.driver,20,1).until(lambda driver:driver.find_element(*loc).is_displayed())
            #返回页面定位元素的值
            return self.driver.find_element(*loc)

        except Exception as e:
            #获取时间戳格式
            data_time=datetime.now().strftime('%Y%m%d %H%M%S')
            #截图带时间戳的
            self.driver.save_screenshot(f'error_{data_time}.png')
            print('元素未定位找到',e)

    #封装输入方法
    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)

    #封装点击的方法
    def click(self, loc):
        self.find_element(loc).click()

    #封装清除文本内容
    def clear(self,loc):
        self.find_element(loc).clear()

    #封装断言方法
    def execp(self,loc):
        return self.find_element(loc).text




